package com.example.myspringboot.caffeine;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.boot.autoconfigure.cache.CacheType;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zheng
 */
@Configuration
public class CaffeineConfig {

//    @Bean
    public CacheManager caffeineCacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        List<CaffeineCache> caffeineCaches = new ArrayList<>();
//
//        for (CacheType cacheType : CacheType.values()) {
//            caffeineCaches.add(new CaffeineCache(cacheType.name(),
//                    Caffeine.newBuilder()
//                            .expireAfterWrite(cacheType.getExpires(), TimeUnit.SECONDS)
//                            .build()));
//        }

        cacheManager.setCaches(caffeineCaches);

        return cacheManager;
    }

}
